#include <iostream>
using namespace std;

int PowerBase10(int n) {
	int result = 1;
	for (int i=0; i<n; i++)
		result *= 10;

	return result;
}

int count(char* num, int len) {
	int first = *num - '0';
	
	if (len==1 && first==0)
		return 0;
	if (len==1 && first>0)
		return 1;

	int numFirstDigit = 0;
	if (first > 1)
		numFirstDigit = PowerBase10(len - 1);
	else if (first == 1)
		numFirstDigit = atoi(num + 1) + 1;

	int numOtherDigits = first*(len-1)*PowerBase10(len-2);
	int numRecursive = count(num+1, len-1);

	return numFirstDigit + numOtherDigits + numRecursive;
}

int number_of_1(int n) {
	char num[100] = {0};
	sprintf(num, "%d", n);

	return count(num, strlen(num));
}

//int main() {
//	int ret = number_of_1(12345);
//	
//	return 0;
//}